unit TIFHandler;

interface

uses
  Classes, SysUtils,
  hyieutils, imageenproc,ieopensavedlg, imageenview, ieview;


Type
  TTifHandler = Class(TComponent)
    private
      FView: TImageEnView;
      FTIF: TIETIFFHandler;
      FStream: TMemoryStream;
      FFileName:string;
      function GetPageCount: integer;
      function GetPages(index: integer): TIELayer;
    public
      procedure Open(FileName:String);
      procedure DeletePage(aPageIndex:integer);
      procedure MovePage(aNewPageIdx, aOrgPageIdx:integer);

      Constructor Create(aOwner:TComponent); override;
      Destructor  Destroy; override;

      property PageCount:integer read GetPageCount;
      property Pages[index:integer]:TIELayer read GetPages;
  End;



implementation

{ TTifHandler }

constructor TTifHandler.Create(aOwner: TComponent);
begin
  inherited;
  FStream := TMemoryStream.Create;
  FView := TImageEnView.Create(self);
  FView.IO.AsyncMode := False;

  FTIF := TIETIFFHandler.Create;
end;

procedure TTifHandler.DeletePage(aPageIndex: integer);
begin
  FTif.DeletePage(aPageIndex);
  FTif.WriteFile(FFileName);
end;

destructor TTifHandler.Destroy;
begin
  FStream.Free;
  FView.Free;

  FTIF.Free;
  inherited;
end;

function TTifHandler.GetPageCount: integer;
begin
  Result := FTIF.GetPagesCount;
end;

function TTifHandler.GetPages(index: integer): TIELayer;
begin
  if (index < 0 ) or ( index >= FView.LayersCount)  then
    Raise Exception.Create( format('%s GetPages : index out of Range',[self.ClassName]) );

  Result := FView.Layers[ index ] ;
end;

procedure TTifHandler.MovePage(aNewPageIdx, aOrgPageIdx: integer);
begin
  FTif.MovePage(aOrgPageIdx, aNewPageIdx);
  FTif.WriteFile(FFileName);
end;

procedure TTifHandler.Open(FileName: String);
var
  I: Integer;
begin
  FFileName := FileName;
  FTIF.ReadFile( FileName );

  FView.Clear;
  FView.LayersClear;
  for I := 0 to FTIF.GetPagesCount - 1 do
  begin
    if i > 0 then FView.LayersAdd;
    FStream.Clear;
    FTIF.WriteStream(FStream, i);
    FStream.Position := 0;

    FView.IO.LoadFromStreamTIFF( FStream );
  end;
end;

end.
